/*
 *       This file is part of VFC.
 *
 *        VFC is free software: you can redistribute it and/or modify
 *        it under the terms of the GNU General Public License as published by
 *        the Free Software Foundation, either version 3 of the License, or
 *        (at your option) any later version.
 *
 *        VFC is distributed in the hope that it will be useful,
 *        but WITHOUT ANY WARRANTY; without even the implied warranty of
 *        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *        GNU General Public License for more details.
 *
 *        You should have received a copy of the GNU General Public License
 *        along with VFC.  If not, see <http://www.gnu.org/licenses/>.
 */


using System;
using System.Collections.Generic;
using System.Text;

namespace VFC.Nes.Cpu.Opcode
{
    class Indirect
    {
        private static int Addr = 0, tmp = 0, tmp2 = 0, MSB = 0, LSB = 0;

        public static void JMP()
        {
            tmp = (Cpu.Core.Param2 << 8) | Cpu.Core.Param1;

            /*      An Indirect jump must never use a vector beginning on the last byte of a page.
             *      So if the target is 0x02ff, then the high byte must came from 0x0200.
             */
            if ((tmp & 0xff) != 0xff)
            {
                tmp2 = (tmp + 1) & 0xffff;
            }
            else
            {
                tmp2 = tmp & 0xff00;
            }

            LSB = Cpu.Memory.Read(tmp);
            MSB = Cpu.Memory.Read(tmp2 | (tmp & 0xff00));
            Addr = (MSB << 8) | LSB;
            Cpu.Register.PC = Addr;
        }
    }
}
